package com.fqgj.xjd.user.service.impl;

import com.fqgj.common.api.Response;
import com.fqgj.xjd.user.client.UserFaceScoreService;
import com.fqgj.xjd.user.client.response.UserFaceScoreRO;
import com.fqgj.xjd.user.common.enums.UserErrorCodeEnum;
import com.fqgj.xjd.user.dao.UserFaceScoreDao;
import com.fqgj.xjd.user.entity.UserFaceScoreEntity;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * ·
 *
 * @author liuming
 * Created on 下午3:15 2018/11/28.
 */
@Service("userFaceScoreService")
public class UserFaceScoreServiceImpl implements UserFaceScoreService {

    @Autowired
    UserFaceScoreDao userFaceScoreDao;

    @Override
    public Response addFaceScore(UserFaceScoreRO userFaceScoreRO) {
        if (userFaceScoreRO == null || StringUtils.isBlank(userFaceScoreRO.getUserCode())) {
            return Response.error(UserErrorCodeEnum.PARAM_ERROR);
        }

        Map<String, Object> map = new HashMap<>();
        map.put("userCode", userFaceScoreRO.getUserCode());
        map.put("deleted", 0);
        List<UserFaceScoreEntity> userFaceScoreEntities = userFaceScoreDao.selectByParams(map);
        if (CollectionUtils.isNotEmpty(userFaceScoreEntities)) {
            UserFaceScoreEntity userFaceScoreEntity = userFaceScoreEntities.get(0);
            UserFaceScoreEntity updateEntity = new UserFaceScoreEntity();
            BeanUtils.copyProperties(userFaceScoreRO, updateEntity);
            updateEntity.setId(userFaceScoreEntity.getId());
            updateEntity.setGmtModified(new Date());
            userFaceScoreDao.updateByPrimaryKey(updateEntity);
            return Response.ok();
        }

        UserFaceScoreEntity userFaceScoreEntity = new UserFaceScoreEntity();
        BeanUtils.copyProperties(userFaceScoreRO, userFaceScoreEntity);
        userFaceScoreDao.insert(userFaceScoreEntity);
        return Response.ok();
    }

    @Override
    public Response deleteByUserCode(String userCode) {
        if (StringUtils.isBlank(userCode)) {
            return Response.error(UserErrorCodeEnum.PARAM_ERROR);
        }

        userFaceScoreDao.deleteByUserCode(userCode);
        return Response.ok();
    }

    @Override
    public Response<UserFaceScoreRO> selectByUserCode(String userCode) {
        if (StringUtils.isBlank(userCode)) {
            return Response.error(UserErrorCodeEnum.PARAM_ERROR);
        }

        Map<String, Object> map = new HashMap<>();
        map.put("userCode", userCode);
        map.put("deleted", 0);
        List<UserFaceScoreEntity> userFaceScoreEntities = userFaceScoreDao.selectByParams(map);
        if (CollectionUtils.isNotEmpty(userFaceScoreEntities)) {
            UserFaceScoreRO userFaceScoreRO = new UserFaceScoreRO();
            BeanUtils.copyProperties(userFaceScoreEntities.get(0), userFaceScoreRO);
            return Response.ok().putData(userFaceScoreRO);
        }

        return Response.ok();
    }
}
